home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / HARDWARE / X-11 / HARDWARE.C next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  5.1 KB  |  137 lines  |  [TEXT/MACA]

  1. /** 3DGPL *************************************************\
  2.  *  (X11, 8bit deep bitmaps, 32bit mashine)               *
  3.  *  Header for hardware specific stuff.                   *
  4.  *                                                        *
  5.  *  Defines:                                              *
  6.  *    HW_open_screen         opening output surface;      *
  7.  *    HW_blit                colourmap onto the screen;   *
  8.  *    HW_close_screen        closing output;              *
  9.  *                                                        *
  10.  *    HW_run_event_loop      runiing for events;          *
  11.  *    HW_quit_event_loop     quiting running.             *
  12.  *                                                        *
  13.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  14.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  15.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  16.  *  WITHOUT AUTHORISATION                                 *
  17. \**********************************************************/
  18.  
  19. #include <X11/Xlib.h>                       /* all X stuff */
  20. #include "../hardware/hardware.h"           /* harware dependent stuff */
  21.  
  22. Display *HW_display;                        /* x server connection */
  23. Visual *HW_visual;                          /* display's visual */
  24. int HW_screen;                              /* deafualt screen */
  25. Colormap HW_cmap;                           /* window's colourmap */
  26. GC HW_gc;                                   /* default graphical conext */
  27. Window HW_window;                           /* window being created */
  28. Window HW_rootw;                            /* the very root window */
  29. XColor HW_rgb[256];                         /* pixels rgb values */
  30. unsigned long HW_cells[256];                /* colour numbers */ 
  31. XImage *HW_image;                           /* the thing containing bitmap */
  32. int HW_running;                             /* event loop still running */
  33.  
  34. /**********************************************************\
  35.  *  Implementations for fast memory operations.           *
  36. \**********************************************************/
  37.                   
  38. void HW_set_int(int *d,long l,int v) {long i; for(i=0;i<l;i++) *d++=v; }
  39.  
  40. /**********************************************************\
  41.  * Creating a window.                                     *
  42.  * RETURNS:  0 on display opening error;                  *
  43.  * --------  1 on success.                                *
  44. \**********************************************************/
  45.  
  46. int HW_open_screen(char *display_name,char *screen_name,
  47.            struct HW_colour palette[256],unsigned char *colourmap
  48.           )
  49. {
  50.  int i;
  51.  
  52.  if((HW_display=XOpenDisplay(display_name))==NULL) return(0);
  53.  HW_screen=DefaultScreen(HW_display);
  54.  HW_visual=DefaultVisual(HW_display,HW_screen);
  55.  HW_rootw=RootWindow(HW_display,HW_screen);
  56.  HW_gc=DefaultGC(HW_display,HW_screen);
  57.  
  58.  HW_window=XCreateSimpleWindow(HW_display,HW_rootw,0,0,
  59.                    HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE,
  60.                    CopyFromParent,CopyFromParent,CopyFromParent
  61.                   );
  62.  
  63.  HW_cmap=XCreateColormap(HW_display,HW_rootw,HW_visual,AllocNone);
  64.  for(i=0;i<256;i++)
  65.   if(!XAllocColorCells(HW_display,HW_cmap,0,0,0,HW_cells+i,1))
  66.    return(0);
  67.   else
  68.   {
  69.    HW_rgb[i].flags= DoRed|DoGreen|DoBlue;
  70.    HW_rgb[i].red=palette[i].hw_r;
  71.    HW_rgb[i].green=palette[i].hw_g;
  72.    HW_rgb[i].blue=palette[i].hw_b;
  73.    HW_rgb[i].pixel=HW_cells[i];
  74.   }
  75.  XStoreColors(HW_display,HW_cmap,HW_rgb,256); 
  76.  
  77.  HW_image=XCreateImage(HW_display,HW_visual,8,ZPixmap,0,
  78.                colourmap,HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE,
  79.                8,HW_SCREEN_X_SIZE); 
  80.  
  81.  XSetWindowColormap(HW_display,HW_window,HW_cmap); 
  82.  XMapWindow(HW_display,HW_window);
  83.  XSync(HW_display,False);                       
  84.  XSelectInput(HW_display,HW_window,KeyPressMask); 
  85.  
  86.  return(1);
  87. }
  88.  
  89. /**********************************************************\
  90.  * blitting a bitmap into the allocated window.           *
  91. \**********************************************************/
  92.  
  93. void HW_blit(void)
  94. {
  95.  XPutImage(HW_display,HW_window,HW_gc,
  96.        HW_image,0,0,0,0,HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE);
  97. }
  98.  
  99. /**********************************************************\
  100.  * Deallocating a window.                                 *
  101. \**********************************************************/
  102.  
  103. void HW_close_screen(void)
  104. {
  105.  XCloseDisplay(HW_display);
  106. }
  107.  
  108. /**********************************************************\
  109.  *  The main event loop.                                  *
  110. \**********************************************************/
  111.  
  112. void HW_run_event_loop(void (*application_main)(void),
  113.                void (*application_key_handler)(int key_code)
  114.               )
  115. {
  116.  XEvent report;                             /* beware putting XKeyEvent here */ 
  117.  
  118.  HW_running=1;
  119.  while(HW_running==1)
  120.  {
  121.   if(XCheckWindowEvent(HW_display,HW_window,KeyPressMask,&report)==1)
  122.    application_key_handler(((XKeyEvent*)&report)->keycode);
  123.   application_main();    
  124.  }
  125. }
  126.  
  127. /**********************************************************\
  128.  *  quiting the event loop.                               *
  129. \**********************************************************/
  130.  
  131. void HW_quit_event_loop(void)
  132. {
  133.  HW_running=0;
  134. }
  135.  
  136. /**********************************************************/
  137.